package com.gfxy.modules.items.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gfxy.modules.items.entity.Zyhy;
import com.gfxy.modules.items.entity.teachingresult.TeachingResultContent;
import com.gfxy.modules.items.mapper.ZyhyMapper;
import com.gfxy.modules.items.service.IZyhyService;
import com.gfxy.modules.items.vo.*;
import com.gfxy.modules.excel.ExcelUtil;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 重要会议和典型案例
 * @Author: jeecg-boot
 * @Date:   2021-10-13
 * @Version: V1.0
 */
@Service
public class ZyhyServiceImpl extends AbstractProjectServiceImpl<ZyhyMapper, Zyhy> implements IZyhyService {
    protected int memberCellNum = 3;

    @Override
    protected void pass(String id, String isPass) {

    }
    @Override
    protected String[] getMemberArray(Row row) {
        String nameStrs = ExcelUtil.formatCell(row.getCell(memberCellNum));
        return nameStrs.split(memberNameSeparator);
    }
    @Override
    protected Zyhy getOneProject(Row row) {

        String date = ExcelUtil.formatCell(row.getCell(0));
//
//        String award= ExcelUtil.formatCell(row.getCell(1));
        String classess  = ExcelUtil.formatCell(row.getCell(1));
        String projectName = ExcelUtil.formatCell(row.getCell(2));

        Zyhy zxjpk = new Zyhy();

        zxjpk.setDate(date);
        zxjpk.setClassess(translateClassess(classess));
        zxjpk.setProjectName(projectName);

        zxjpk.setIsPass(0);
        //这个地方不要忘了-算总分
        zxjpk.setTotalScore(this.getTotalScore(zxjpk));
        return zxjpk;
    }

    @Override
    public Double getTotalScore(Zyhy zyhy) {
        Integer classess = zyhy.getClassess();
        double result = 0.0;
        if(classess == 1){
            result = 75;
        } else if(classess == 2){
            result = 25;
        }
        return result;
    }

//    @Override
//    public Double[] getMemberListScore(double totalScore, int memberNum) {
//        return new Double[0];
//    }

    @Override
    public IPage<ZyhyMemberPage> getZyhyMemberPageListByYear(String date, Integer pageNo, Integer pageSize) {
        Page<ZyhyMemberPage> zxjpkMemberPagePage = new Page<>(pageNo, pageSize);
        return zxjpkMemberPagePage.setRecords(this.baseMapper.selectZyhyVoListByYear(date));
    }
    @Override
    public List<ZyhyMemberPage> getZxjpkMemberPageListByYear(String date) {
        return this.baseMapper.selectZyhyVoListByYear(date);
    }
    protected double computeProportion(int ranked, int totalNum) {

        if (totalNum == 1) {
            return 1.0;
        } else if (totalNum == 2) {
            if (ranked == 1) {
                return 0.6;
            } else if (ranked == 2) {
                return 0.4;
            } else {
                return 0.0;
            }
        } else if (totalNum == 3) {
            if (ranked == 1) {
                return 0.5;
            } else if (ranked == 2) {
                return 0.3;
            } else if (ranked == 3) {
                return 0.2;
            } else {
                return 0.0;
            }
        } else if (totalNum == 4) {
            if (ranked == 1) {
                return 0.5;
            } else if (ranked == 2) {
                return 0.25;
            } else if (ranked == 3) {
                return 0.15;
            } else if (ranked == 4) {
                return 0.1;
            } else {
                return 0.0;
            }
        } else if (totalNum == 5) {
            if (ranked == 1) {
                return 0.45;
            } else if (ranked == 2) {
                return 0.2;
            } else if (ranked == 3) {
                return 0.15;
            } else if (ranked == 4) {
                return 0.1;
            } else if (ranked == 5) {
                return 0.1;
            } else {
                return 0.0;
            }
        } else if (totalNum == 6) {
            if (ranked == 1) {
                return 0.45;
            } else if (ranked == 2) {
                return 0.2;
            } else if (ranked == 3) {
                return 0.15;
            } else if (ranked == 4) {
                return 0.10;
            } else if (ranked == 5) {
                return 0.05;
            } else if (ranked == 6) {
                return 0.05;
            } else {
                return 0.0;
            }
        } else if (totalNum >= 7) {
            if (ranked == 1) {
                return 0.45;
            } else if (ranked == 2) {
                return 0.2;
            } else if (ranked == 3) {
                return 0.15;
            } else if (ranked > 4) {
                return Math.floor(0.2 * 10000 / (totalNum - 3)) / 10000.0;
            }else {
                return 0.0;
            }
        } else {
            return 0.0;
        }

    }

//    @Override
//    public List<String> importExcel(MultipartFile file) throws IOException {
//        List<String> result = new ArrayList<>();
//        InputStream inputStream = file.getInputStream();
//        if (!(inputStream).markSupported()) {
//            inputStream = new PushbackInputStream(inputStream, 8);
//        }
//
//        Workbook workbook = WorkbookFactory.create(inputStream);
//
//        Sheet sheet = workbook.getSheetAt(0);
//        for(int i = 1; i<sheet.getLastRowNum();i++){
//            Row row = sheet.getRow(i);
//            String year = ExcelUtil.formatCell(row.getCell(0));
//            int classess = this.translateClassess(ExcelUtil.formatCell(row.getCell(1)));
//            String projectName = ExcelUtil.formatCell(row.getCell(2));
//
//
//            Zyhy zyhy = new Zyhy();
//            zyhy.setClassess(classess);
//            zyhy.setYear(year);
//            zyhy.setProjectName(projectName);
//
//            String name = ExcelUtil.formatCell(row.getCell(3));
//            List<MemberList> memberLists = memberListMapper.selectByRealName(name.trim());
//            MemberList resultMember = null;
//            if(memberLists.size()==1){
//
//                MemberList memberList = memberLists.get(0);
//                memberList.setRanked(1);
//                memberList.setProjectType("12");
//                memberList.setIsCustom("0");
//                memberList.setProportion(1.00);
//                resultMember = memberList;
//
//            } else if(memberLists.size() == 0){
//
//                result.add("教师:" + name + " 数据不存在，请手动处理");
//                MemberList memberList = new MemberList();
//                resultMember = memberList;
//            } else if(memberLists.size()>1){
//
//                result.add("教师:" + name + " 存在同名教师，请手动处理");
//                MemberList memberList = new MemberList();
//                resultMember = memberList;
//            }
//
//            double totalScore = this.getTotalScore(zyhy.getClassess());
//            zyhy.setTotalScore(totalScore);
//
//            this.saveMain(zyhy, resultMember);
//        }
//
//
//
//        if(result.size()==0){
//            result.add("导入成功");
//        }
//
//        return result;
//    }

//
    private int translateClassess(String name){
        if(name == null || name.equals("")){
            return 0;
        } else if(name.equals("国家级")){
            return 1;
        } else if(name.equals("省级")){
            return 2;
        }else {
            return 0;
        }
    }

    @Override
    protected List<TeachingResultContent> createTeachingResultContent(Zyhy jxcg)  {

        List<TeachingResultContent> result = new ArrayList<>();

        // 类型
        TeachingResultContent classTeachingResultContent = new TeachingResultContent();
        classTeachingResultContent.setType(1);
        classTeachingResultContent.setLevel(jxcg.getClassess());

        try {
            classTeachingResultContent.setValue(dictTranslater.getDictValue(ZyhyMemberPage.class, "classess", String.valueOf(jxcg.getClassess())));
            result.add(classTeachingResultContent);
        } catch (NoSuchFieldException e) {
            log.error(e.getMessage(),e);
        }


        return result;
    }

}
